﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class Utility  {
    public static int TimeToFrame(float time)
    {
        return (int) Math.Ceiling((double) time / Time.fixedDeltaTime);//只要有小数都加1
    }

    public float FrameToTime(int frame)
    {
        return (float) Time.fixedDeltaTime * frame;
    }

    public static GameObject FindChild(GameObject p, string path)
    {
        if (p != null)
        {
            Transform transform = p.transform.Find(path);
            return (transform != null) ? transform.gameObject : null;
        }
        return null;
    }

    public static T GetComponentInChild<T>(GameObject p, string path) where T : MonoBehaviour
    {
        if (p == null)
        {
            return null;
        }
        Transform transform = p.transform.Find(path);
        if (transform)
        {
            return transform.GetComponent<T>();
        }
        return null;
    }

    public static GameObject FindChildByName(GameObject root, string childname)
    {
        if (root == null)
        {
            return null;
        }
        for (int i = 0; i < root.transform.childCount; i++)
        {
            if (root.transform.GetChild(i).name == childname)
            {
                return root.transform.GetChild(i).gameObject;
                break;
            }
        }
        return null;
    }

    public static GameObject FindChildByName(Component component, string childname)
    {
        return FindChildByName(component.gameObject, childname);
    }

    public static void SetChildrenActive(GameObject root,bool active)
    {
        for (int i = 0; i < root.transform.childCount; i++)
        {
            root.transform.GetChild(i).gameObject.SetActive(active);
        }
    }

    public static void StringToByteArray(string str, ref byte[] buffer)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(str);
        Array.Copy(bytes,buffer,Math.Min(bytes.Length,buffer.Length));
        buffer[buffer.Length - 1] = 0;//标志位 最后是0
    }

    public static string UTF8Convert(string str)
    {
        return str;
    }

    public static string UTF8Convert(byte[] p)
    {
        string result;
        try
        {
            result = (p != null) ? Encoding.UTF8.GetString(p).TrimEnd() : null;
        }
        catch (Exception e)
        {
            result = null;
        }
        return result;
    }

    public static byte[] BytesConvert(string s)
    {
        return Encoding.UTF8.GetBytes(s);
    }

    public static byte[] SByteArrToByte(sbyte[] p)
    {
        byte[] array = new byte[p.Length];
        for (int i = 0; i < p.Length; i++)
        {
            array[i] = (byte) p[i];
        }
        return array;
    }

    public static string UTF8Convert(sbyte[] p)
    {
        byte[] bytes = SByteArrToByte(p);
        return UTF8Convert(bytes);
    }

    public static int JaveHashCode(string str)
    {
        int num = 0;
        int lenght = str.Length;
        if (lenght > 0)
        {
            char[] arr = str.ToCharArray();
            for (int i = 0; i < lenght; i++)
            {
                char c = arr[i];
               
                num = 31 * num + (int) c;
            }
        }
        return num;
    }
    public static int JavaHashCodeIgnoreCase( string str)
    {
        int num = 0;
        int length = str.Length;
        if (length > 0)
        {
            int num2 = 0;
            char[] srr = str.ToCharArray();
            for (int i = 0; i < length; i++)
            {
                char c = srr[num2++];
                if (c >= 'A' && c <= 'Z')
                {
                    c += ' ';
                }
                num = 31 * num + (int)c;
            }
        }
        return num;
    }
}






















































